package com.example.login.Controller;

import com.example.login.Mapper.GoodsMap;
import com.example.login.Pojo.*;
import com.example.login.Service.ItemService;
import com.example.login.Service.MP3Service;
import com.example.login.Service.MobileService;
import com.example.login.Service.PCService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

@Controller
public class UploadController {
    @Value("${ImagePath}")
    String filePath;
    @Autowired
    public PCService pcService;
    @Autowired
    public MP3Service mp3Service;
    @Autowired
    public MobileService mobileService;
    @Autowired
    public ItemService  itemService;
    @Autowired
    public GoodsMap goodsMap;
    boolean judge;
    @PostMapping("/pcUpload")
      public String pcUpload(@ModelAttribute("pc") PC pc,
                             @ModelAttribute("good") Goods good,
                         @RequestParam("file") MultipartFile file,
                         Model model, HttpSession session){

        Seller seller =(Seller)session.getAttribute("seller");
        //添加
        judge=pcService.Add(pc.getColor()+pc.getROM()+pc.getRAM(),pc.getColor(),pc.getROM(),pc.getRAM(),
                file.getOriginalFilename(), pc.getPrice(),pc.getNum(),seller.getId(),good.getItem_name(),
                seller.getStore_name(),good.getBrand_name(),good.getFright(),pc.getCombination(),pc.getIntroduction());
        //有重复商品了
        if(!judge){
            model.addAttribute("msg","上架商品重复，请重新选择");
            return "商家中心/上架电脑商品";
        }
        else{
            model.addAttribute("msg","添加成功");
        }
        int id=goodsMap.getIdBySellerIdAndName(seller.getId(),good.getItem_name());
        String fileDirPath = "src/main/resources/" + filePath + "//" + id+pc.getColor()+pc.getROM()+pc.getRAM();
        store(fileDirPath,file);
        return "商家中心/上架电脑商品";
    }
    @RequestMapping("/jump")
    public String jump(){
        return "登录";
    }
    @PostMapping("/mp3Upload")
    public String mp3Upload(@ModelAttribute("mp3") MP3 mp3,
                            @ModelAttribute("good") Goods good,
                            @RequestParam("file") MultipartFile file,
                            Model model, HttpSession session)
    {
        //构建上传文件的存放 "文件夹" 路径
        Seller seller =(Seller)session.getAttribute("seller");
        System.out.println(good);
        System.out.println(mp3);
        //添加
        judge=mp3Service.Add(mp3.getColor()+mp3.getROM(),mp3.getColor(),mp3.getROM(),
                file.getOriginalFilename(),
                mp3.getPrice(),mp3.getNum(),seller.getId(),good.getItem_name(),
                seller.getStore_name(),good.getBrand_name(),good.getFright(),mp3.getCombination(),mp3.getIntroduction());
        if(!judge){
            model.addAttribute("msg","上架商品重复，请重新选择");
            return "商家中心/上架MP3商品";
        }
        else{
            model.addAttribute("msg","添加成功");
        }
        int id=goodsMap.getIdBySellerIdAndName(seller.getId(),good.getItem_name());
        System.out.println(itemService.getMp3Goods());
        String fileDirPath = "src/main/resources/" + filePath + "//" + id+mp3.getColor()+mp3.getROM();
        store(fileDirPath,file);

        return "商家中心/上架MP3商品";
    }
    @PostMapping("/mobileUpload")
    public String mobileUpload(@ModelAttribute("mobile") MobilePhone mobile,
                               @ModelAttribute("good") Goods good,
                               @RequestParam("file") MultipartFile file,
                               Model model, HttpSession session)
    {
        //构建上传文件的存放 "文件夹" 路径
        Seller seller =(Seller)session.getAttribute("seller");
        System.out.println(good);
        System.out.println(mobile);
        //添加
        judge=mobileService.Add(mobile.getColor()+mobile.getROM()+mobile.getRAM()+mobile.getNetwork(),mobile.getColor(),mobile.getROM(),mobile.getRAM(),
                file.getOriginalFilename(),
                mobile.getPrice(),mobile.getNum(),mobile.getNetwork(),seller.getId(),good.getItem_name(),
                seller.getStore_name(),good.getBrand_name(),good.getFright(),mobile.getCombination(),mobile.getIntroduction());
        //如果重复添加
        if(!judge){
            model.addAttribute("msg","上架商品重复，请重新选择");
            return "商家中心/上架手机商品";
        }
        else{
            model.addAttribute("msg","添加成功");
        }
        int id=goodsMap.getIdBySellerIdAndName(seller.getId(),good.getItem_name());
        String fileDirPath = "src/main/resources/" + filePath + "//" + id+mobile.getColor()+mobile.getROM()+mobile.getRAM()+mobile.getNetwork();
        store(fileDirPath,file);
        return "商家中心/上架手机商品";
    }
    public void store(String fileDirPath,MultipartFile file)
    {
        File fileDir = new File(fileDirPath);

        if(!fileDir.exists()){
            // 递归生成文件夹
            fileDir.mkdirs();
        }
        try {
            File image= new File(fileDir.getAbsolutePath() + File.separator + file.getOriginalFilename());
            System.out.println(file.getOriginalFilename());
            file.transferTo(image);
            System.out.println(image.getAbsolutePath());//绝对路径
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
